package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author huangqicai
 * @since 2022-10-21
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduChapterMapper chapterMapper;

    @Autowired
    private EduVideoMapper videoMapper;

    /*根据课程id获取章节信息*/
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        /*1.根据课程id查询课程下面的所有章节*/
        QueryWrapper<EduChapter> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("course_id", courseId);
        List<EduChapter> eduChapters = chapterMapper.selectList(wrapper1);
        /*2.根据课程id查询课程下面的所有小节*/
        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("course_id", courseId);
        List<EduVideo> eduVideos = videoMapper.selectList(wrapper2);
        ArrayList<ChapterVo> chapterVos = new ArrayList<>();
        /*遍历章节*/
        for (int i = 0; i < eduChapters.size(); i++) {
            EduChapter eduChapter = eduChapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            ArrayList<VideoVo> childrenLists = new ArrayList<>();
            for (int j = 0; j < eduVideos.size(); j++) {
                EduVideo eduVideo = eduVideos.get(j);
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    childrenLists.add(videoVo);
                }
            }
            chapterVo.setChildren(childrenLists);
            chapterVos.add(chapterVo);
        }
        /*在遍历章节的循环里面遍历小节，让小节和章节进行匹配*/
        return chapterVos;
    }


    /*根据章节id删除章节*/
    @Override
    public boolean deleteChapter(String chapterId) {
        /*根据章节id去数据库里面查询是否有小节*/
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        Integer count = videoMapper.selectCount(wrapper);
        int res;
        if(count==0){
            /*删除章节*/
            res = baseMapper.deleteById(chapterId);
        }else {
            throw new GuliException(20001,"不能删除");
        }
        return res>0;
    }

    /*根据课程id删除课程里面的章节信息*/
    @Override
    public boolean deleteChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        int delete = baseMapper.delete(wrapper);
        if(delete>0){
            return true;
        }
        return false;
    }

}
